package shanghaielectric_lib.Utils;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.shanghai.shanghaielectric.Constants;
import com.shanghai.shanghaielectric.MyApplication;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by 45000460 on 2017/11/14.
 */

public class MySystemUtil {

    public static String getMethonNmae(Class<?> callingClass) {
        String className = callingClass.getSimpleName();
        StackTraceElement[] s = Thread.currentThread().getStackTrace();
        String methodName = s[3].getMethodName();
        return className + ":" + methodName;
    }

    public static String getSystemTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日   HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());

        return formatter.format(curDate);
    }

    public static void installAPK(Context context, File savedFile) {
        //调用系统的安装方法
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(savedFile), "application/vnd.android.package-archive");
        context.startActivity(intent);

    }


    public static String formatLong2StringTime(long time) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日   HH:mm:ss");
        Date curDate = new Date(time);

        return formatter.format(curDate);
    }

    public static void HideKeyboard(View v) {
        InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive()) {
            imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);

        }
    }
//  false 关闭导航栏 true 显示导航栏
public static void setNavigationState(boolean flag) {
        SharedPreferences sharedPreferences = MyApplication.getContext()
                .getSharedPreferences("NavigationState", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("state", flag);
        editor.commit();
    }

    public static boolean getNavigationState() {
        SharedPreferences sharedPreferences = MyApplication.getContext()
                .getSharedPreferences("NavigationState", Context.MODE_PRIVATE);
        boolean flag = sharedPreferences.getBoolean("state", false);

        return flag;
    }


    public static void setPassWords(String keys) {
        SharedPreferences sharedPreferences = MyApplication.getContext()
                .getSharedPreferences("PassWords", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("keys", keys);
        editor.commit();
    }

    private static String getPassWords() {
        SharedPreferences sharedPreferences = MyApplication.getContext()
                .getSharedPreferences("PassWords", Context.MODE_PRIVATE);
        return sharedPreferences.getString("keys", Constants.USER_PASSWORDS);
    }

    public static boolean checkPassWords(String inputKeys) {
        return  getPassWords().equals(inputKeys);
    }


}
